import http from "http";
import fs from "fs";
import path from "path";
const services = http.createServer(async function (req, res) {
  const reqestUrl = req.url;
  if (reqestUrl.startsWith("/api")) {
    const pathList = reqestUrl.split("/");
    const interfacePath = pathList.slice(2).join("/");
    let filePath = path.join(import.meta.dirname, "/src/mockdata/");
    filePath = filePath + interfacePath + ".js";
    if (fs.existsSync(filePath)) {
      const fileContent = await fs.readFileSync(filePath, "utf-8");
      const data = eval(fileContent);
      res.setHeader("Content-Type", "application/json");
      res.end(JSON.stringify(data));
    } else {
      res.end(
        JSON.stringify({
          data: null,
          message: `${interfacePath}接口不存在`,
          code: 200
        })
      );
    }
  }
});
services.listen(3000, function () {
  console.log(`3000端口已经启动。。。。。。。。。`);
});
